package net.godbirds.api;

import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import net.godbirds.Constant;
import net.godbirds.aps.WelcomeService;
import net.godbirds.domain.model.TDefUser;

@RestController
public class WelcomeController {

	@Autowired
	WelcomeService welcomeService;
	
	/**若所有微服务都希望控制层控制，则可将其移到制定的view路径下**/
	@RequestMapping("/")
	public ModelAndView main(){
		return new ModelAndView("index");
	}
	/**若所有微服务都希望控制层控制，则可将其移到制定的view路径下**/
	@RequestMapping(value = "/homeUrl",method=RequestMethod.GET)
	public ModelAndView home(){
		return new ModelAndView("home");
	}
	/**若所有微服务都希望控制层控制，则可将其移到制定的view路径下**/
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/exit",method=RequestMethod.GET)
	public ModelAndView exit(HttpServletRequest request){
		ModelAndView mav = new ModelAndView("index");
		String msg = request.getParameter("msg");
		if(msg == null) {
			msg = (String) request.getAttribute("msg");
		}
		mav.addObject("msg", msg);
        System.out.println("- 安全退出系统 ...");
		Enumeration es = request.getSession().getAttributeNames();
		while(es.hasMoreElements()){
			String sessionName = (String) es.nextElement();
			System.out.println("======> 清空 Session : "+sessionName+" ... ");
			request.getSession().removeAttribute(sessionName);
		}
		Enumeration er = request.getAttributeNames();
		while(es.hasMoreElements()){
			String requestName = (String) er.nextElement();
			System.out.println("======> 清空 Request : "+requestName+" ... ");
			request.getSession().removeAttribute(requestName);
		}
		msg = null;
		return mav;
	}
	@RequestMapping(value = "/attachUrl",method=RequestMethod.GET)
	public ModelAndView attachUrl(){
		return new ModelAndView("attach");
	}
	/**
	 * 开发中统一界面
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/developUrl",method=RequestMethod.GET)
	public ModelAndView developUrl(HttpServletRequest request){
		String func = request.getParameter("func");
		ModelAndView mav = new ModelAndView("common/develop");
		mav.addObject("func",func);
		return mav;
	}
	@RequestMapping(value="/welcome",method=RequestMethod.GET)
	public ModelAndView welcome(HttpServletRequest requst){
		ModelAndView mav = new ModelAndView("welhome");
		TDefUser tUser = (TDefUser) requst.getSession().getAttribute(Constant.SESSION_USER_KEY);
		if(tUser == null) {
			mav.addObject("msg", "已超时，请重新登录。");
			mav.setViewName("redirect:exit");
		}
		return mav;
	}
	@RequestMapping(value="/attachOk",
			method=RequestMethod.POST,
			produces="application/json;charset=utf-8")
	public Map<String,Object> attachOk(@RequestBody TDefUser tUser){
		Map<String,Object> rtvMap = welcomeService.attach(tUser);
		return rtvMap;
	}
	@RequestMapping(value="/login",
			method=RequestMethod.POST,
			produces="application/json;charset=utf-8")
	@ResponseBody
	public Map<String,Object> login(HttpServletRequest requst,@RequestBody TDefUser tUser){
		Map<String,Object> rtvMap = welcomeService.login(tUser);
		if(rtvMap.get("tUser") == null) {
			rtvMap.put("error", "1");
			rtvMap.put("msg", rtvMap.get("msg"));
			return rtvMap;
		}
		requst.getSession().setAttribute(Constant.SESSION_USER_KEY, rtvMap.get("tUser"));
		return rtvMap;
	}
}
